package org.qiaice.controller;

import lombok.RequiredArgsConstructor;
import org.qiaice.entity.Employee;
import org.qiaice.entity.dto.EmployeeDTO;
import org.qiaice.entity.dto.EmployeeLoginDTO;
import org.qiaice.entity.dto.EmployeePageQueryDTO;
import org.qiaice.entity.dto.PasswordEditDTO;
import org.qiaice.entity.vo.EmployeeLoginVO;
import org.qiaice.result.PageResult;
import org.qiaice.result.Result;
import org.qiaice.service.EmployeeService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/admin/employee", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeService employeeService;

    @PostMapping(value = "/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        return Result.ok(employeeService.login(employeeLoginDTO.getUsername(), employeeLoginDTO.getPassword()));
    }

    @GetMapping(value = "/page")
    public Result<PageResult<Employee>> page(EmployeePageQueryDTO employeePageQueryDTO) {
        return Result.ok(employeeService.pageQuery(
                employeePageQueryDTO.getName(), employeePageQueryDTO.getPage(),
                employeePageQueryDTO.getPageSize()));
    }

    @GetMapping(value = "/{id}")
    public Result<Employee> queryById(@PathVariable Long id) {
        return Result.ok(employeeService.getById(id));
    }

    @PostMapping(value = "/status/{status}")
    public Result<Object> changeStatus(@PathVariable Integer status, Long id) {
        employeeService.changeStatus(status, id);
        return Result.ok(null);
    }

    @PostMapping
    public Result<Object> saveEmployee(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.saveEmployee(employeeDTO);
        return Result.ok(null);
    }

    @PostMapping(value = "/logout")
    public Result<Object> logout() {
        return Result.ok(null);
    }

    @PutMapping(value = "/editPassword")
    public Result<Object> changePassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        employeeService.changePassword(
                passwordEditDTO.getEmpId(), passwordEditDTO.getOldPassword(),
                passwordEditDTO.getNewPassword());
        return Result.ok(null);
    }

    @PutMapping
    public Result<Object> change(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.change(employeeDTO);
        return Result.ok(null);
    }
}
